package com.liuya.safe.model;

import com.liuya.base.BaseModel;
import com.liuya.db.script.UserCategory;
import com.liuya.entitle.UserCategoryTestResult;
import com.liuya.safe.policy.service.QueryManagerService;

import java.util.Date;
import java.util.Map;

public class SafeUserCategory extends BaseModel {
    public final static String RESERVED_USER_CATEGORY_ID = "-10";
    private Date installDate;
    private String fileName;
    private Integer status;
    private String xmlContent;
    private UserCategory scriptUserCategory;
    //当为1时表示此用户类型是由角色添加过来的
    private Integer isRole = 1;

    public UserCategory getScriptUserCategory() {
        return scriptUserCategory;
    }

    public void setScriptUserCategory(UserCategory scriptUserCategory) {
        this.scriptUserCategory = scriptUserCategory;
    }

    public String getXmlContent() {
        return xmlContent;
    }

    public void setXmlContent(String xmlContent) {
        this.xmlContent = xmlContent;
    }

    public Date getInstallDate() {
        return installDate;
    }

    public void setInstallDate(Date installDate) {
        this.installDate = installDate;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName == null ? null : fileName.trim();
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Integer getIsRole() {
        return isRole;
    }

    public void setIsRole(Integer isRole) {
        this.isRole = isRole;
    }

    public UserCategoryTestResult test(SafeUser user, Map context, QueryManagerService queryManager) {
        return scriptUserCategory.test(user, context, queryManager);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", id=").append(getId());
        sb.append(", name=").append(getName());
        sb.append(", description=").append(getDescription());
        sb.append(", installDate=").append(installDate);
        sb.append(", fileName=").append(fileName);
        sb.append(", pid=").append(getPid());
        sb.append(", isLeaf=").append(getIsLeaf());
        sb.append(", status=").append(status);
        sb.append("]");
        return sb.toString();
    }

    @Override
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (that == null) {
            return false;
        }
        if (getClass() != that.getClass()) {
            return false;
        }
        SafeUserCategory other = (SafeUserCategory) that;
        return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
            && (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName()))
            && (this.getDescription() == null ? other.getDescription() == null : this.getDescription().equals(other.getDescription()))
            && (this.getInstallDate() == null ? other.getInstallDate() == null : this.getInstallDate().equals(other.getInstallDate()))
            && (this.getFileName() == null ? other.getFileName() == null : this.getFileName().equals(other.getFileName()))
            && (this.getPid() == null ? other.getPid() == null : this.getPid().equals(other.getPid()))
            && (this.getIsLeaf() == null ? other.getIsLeaf() == null : this.getIsLeaf().equals(other.getIsLeaf()))
            && (this.getStatus() == null ? other.getStatus() == null : this.getStatus().equals(other.getStatus()));
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
        result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
        result = prime * result + ((getDescription() == null) ? 0 : getDescription().hashCode());
        result = prime * result + ((getInstallDate() == null) ? 0 : getInstallDate().hashCode());
        result = prime * result + ((getFileName() == null) ? 0 : getFileName().hashCode());
        result = prime * result + ((getPid() == null) ? 0 : getPid().hashCode());
        result = prime * result + ((getIsLeaf() == null) ? 0 : getIsLeaf().hashCode());
        result = prime * result + ((getStatus() == null) ? 0 : getStatus().hashCode());
        return result;
    }
}